/**
 * <p>Title: ServiceZMCL01.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午04:55:38  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCL;
import com.baosight.sczx.zm.cl.domain.ZMCLRoundcheckWorknote;
import com.baosight.sczx.zm.cl.domain.ZMCLSecurityPath;

/**
 * <p>Title: ServiceZMCL01.java</p>
 * <p>Description: 巡检工单管理的服务类</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午04:55:38  张小飞  Create
 *</pre>
 */
public class ServiceZMCL01 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLRoundcheckWorknote</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午02:29:24 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = null;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = this.query(inInfo);
		
		String inspectionUnit = SystemSettingCL.getInstance().getManageDepartId();//安全主管单位ID获取
		outInfo.set("inspectionUnit", inspectionUnit);//巡检单位默认填入安全管理部门
		String noteCode = this.getCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdNotecode");
		outInfo.set("noteCode", noteCode);//工单编号给出默认值
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.noteSource", "cmes.cl.noteSourceInqu", true));//查询区：工单来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.noteSource"));//新增：工单来源
		outInfo.addBlock(getSecurityPath());//巡检路线
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//责任单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.responsibilityPeople", "empCode", "empName", false));//巡检人
		
		return outInfo;

	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLRoundcheckWorknote</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午02:29:24 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCLRoundcheckWorknote.query", new ZMCLRoundcheckWorknote());
		return outInfo;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLRoundcheckWorknote</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午02:29:24 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验编号是否重复
		boolean validateValue = validate(inInfo, "ZMCLRoundcheckWorknote.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CL_ROUNDCHECK_WORKNOTE"));//主键
			EiInfo outInfo = super.insert(inInfo, "ZMCLRoundcheckWorknote.insert");
			String noteCode = this.getCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdNotecode");
			outInfo.set("noteCode", noteCode);//工单编号给出默认值
			return outInfo;
		}
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLRoundcheckWorknote</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午02:29:24 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验编号是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId"));
		boolean validateValue = validate(inInfo, "ZMCLRoundcheckWorknote.validate", params);
		
		if(validateValue) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCLRoundcheckWorknote.update");
			return outInfo;
		}
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLRoundcheckWorknote</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午02:29:24 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCLRoundcheckWorknote.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        String noteCode = this.getCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdNotecode");
		outInfo.set("noteCode", noteCode);//工单编号给出默认值
		return outInfo;
	}
	
	/**
	 * <p>Function: billing</p>
	 * <p>Description: 发单：状态为草拟的记录，状态修改为发单。若勾选的记录中有非草拟状态的记录，状态不变</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-29下午04:55:19 张小飞
	 * </p>
	 *
	 */
	public EiInfo billing(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCLRoundcheckWorknote.updateBill");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: getSecurityPath</p>
	 * <p>Description: 获取巡检路线的block</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-29下午05:00:26 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private EiBlock getSecurityPath() {
		
		EiBlock block = new EiBlock("cmes.cl.securityPath");

		EiColumn eiColumn = new EiColumn("fdKeyId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("fdPathName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		List<Map> _list = dao.query("ZMCLSecurityPath.queryInterface", null);

		block.setRows(_list);
		return block;
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 校验编号是否重复</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-19上午10:35:36 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdNotecode = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdNotecode");
		params.put("fdNotecode", fdNotecode);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLRoundcheckWorknote _temp = (ZMCLRoundcheckWorknote)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNotecode() != null && _temp.getFdNotecode().equals(fdNotecode)) {
				msg.append(" 编号 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	public EiInfo getCode(EiInfo inInfo)
	{
		EiInfo Info = super.query(inInfo, "ZMCLRoundcheckWorknote.queryLatestCode", new ZMCLRoundcheckWorknote());
		EiInfo outInfo = new EiInfo();
		outInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCLRoundcheckWorknote().eiMetadata);
		if(Info.getBlock("result").getRowCount() == 0)
		{
			String defaultStr = "AQXJGD" + "_" + StaticTools.getDateDefault()+ "_" + "001";//如果在当天没有记录生成，则默认为是当天的第一条记录
			outInfo.getBlock("result").setCell(0,"fdNotecode",defaultStr );
		}else
		{
			String str = Info.getBlock(EiConstant.resultBlock).getCell(0, "fdNotecode").toString();
			outInfo.getBlock("result").setCell(0,"fdNotecode", convertStr(str));
		}
		return outInfo;
	}
	
	private String convertStr(String str)
	{
		String str1 = str.substring(0,16);//取得编号的前16位AQXJGD_YYYYMMDD_
		String str2 = str.substring(16,19);//取得编号的后3位
		int nub = Integer.parseInt(str2); 
		str2 = Integer.toString(nub+1);//自增操作
		if(str2.length() == 1){
			return str1+"00"+str2;//如果是一位数据的话前两位补0
		}else if (str2.length() == 2)
		{
			return str1+"0"+str2;//如果是两位数据的话前一位补0
		}else
		{
			return str1+str2;
		}
	}
	
	/**
	 * 
	 * <p>Function: queryForCustomview</p>
	 * <p>Description: 巡检路线的界面视图customview</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午06:01:44 张小飞
	 * </p>
	 *
	 */
	public EiInfo queryForCustomview(EiInfo inInfo)
	{
		EiInfo outInfo = super.query(inInfo, "ZMCLSecurityPath.queryForCustomview", new ZMCLSecurityPath());
		return outInfo;
	}
}
